package com.yuoj.yuojcodesandbox.controller;

import com.yuoj.yuojcodesandbox.model.ExecuteCodeRequest;
import com.yuoj.yuojcodesandbox.model.ExecuteCodeResponse;
import com.yuoj.yuojcodesandbox.sandbox.java.JavaNativeCodeSandbox;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 刘渠好
 * @since 2025-03-18 21:37
 * 提供API接口给第三方使用
 */
@RestController
public class CodeSandboxController {
    //鉴定请求头和秘钥
    public static final String AUTH_REQUEST_HEADER="auth";

    public static final String AUTH_REQUEST_SECRET="secretKey";

    @Resource
    private JavaNativeCodeSandbox codeSandbox;

    @PostMapping("/executeCode")
    public ExecuteCodeResponse executeCode(@RequestBody ExecuteCodeRequest codeRequest
    , HttpServletRequest request, HttpServletResponse response) {
        if (!AUTH_REQUEST_SECRET.equals (request.getHeader (AUTH_REQUEST_HEADER))){
            response.setStatus (403);
            return null;
        }
        if (codeRequest==null){
            throw new IllegalArgumentException ("请求参数为空!");
        }
        return codeSandbox.executeCode (codeRequest);
    }

}
